package com.ericsson.eritask.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.RollbackException;
import javax.validation.ConstraintViolationException;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ericsson.eritask.dao.SaveTaskDao;

import com.ericsson.eritask.domain.Project;
import com.ericsson.eritask.domain.Reminder;
import com.ericsson.eritask.domain.Task;
import com.ericsson.eritask.domain.User;
import com.ericsson.eritask.domain.UserTask;
import com.ericsson.eritask.dto.ProjectDTO;
import com.ericsson.eritask.dto.ReminderDTO;
import com.ericsson.eritask.dto.TaskDTO;
import com.ericsson.eritask.dto.UserTaskDTO;

@Repository
public class SaveTaskDaoImpl implements SaveTaskDao{
	
	@PersistenceContext
	EntityManager entityManager;

	@Transactional
	@Override
	public boolean insertTask(ProjectDTO projDto, TaskDTO taskDto, ReminderDTO reminderDTO) throws Exception{
		// TODO Auto-generated method stub\
		
		boolean flag = false;
		
		System.out.println("entitymanager = "+entityManager);
		Task task = new Task();
		//Project proj = new Project();
		Reminder reminder = new Reminder();
		Query query = entityManager.createQuery("select p from Project p where p.projectName = :projectName",Project.class);

		query.setParameter("projectName", projDto.getProjectName());
		List<Project> list = query.getResultList( );

		for( Project pr:list ) {
			int pId=pr.getProjectId();
			System.out.print("Project ID FROM LIST :" +pId );
		
			
		//Project proj = entityManager.find(Project.class, pId);
		
		
		//proj.setProjectId(projDto.getProjectId());
		//proj.setProjectName(projDto.getProjectName());
		
		System.out.println("----------------------in dao---------------------------------");
		System.out.println("Project id = "+pr.getProjectId());
		System.out.println("Project Name = "+pr.getProjectName());
		
		task.setProject(pr);
		//task.getProject().setProjectId(projDto.getProjectId());
		task.setDueDate(taskDto.getDueDate());
		task.setPercentageComplete(taskDto.getPercentageComplete());
		task.setPriority(taskDto.getPriority());
		
		task.setStartDate(taskDto.getStartDate());
		task.setStatus(taskDto.getStatus());
		task.setSubject(taskDto.getSubject());
		
		task.setReminder(taskDto.getReminder());
		reminder.setDate(reminderDTO.getReminderDate());
		reminder.setTime(reminderDTO.getReminderTime());
		
		List<Reminder> reminderList = new ArrayList<Reminder>();
		reminderList.add(reminder);		
		
		reminder.setTask(task);
		
		task.setReminders(reminderList);
		//entityManager.persist(proj);
		try
		{
			entityManager.persist(task);
			//entityManager.flush();
			flag = true;
		}
		
		catch (PersistenceException p) {
			throw new Exception(p);
			
			
		}
		catch(Exception e)		
		{   System.out.println("error occured during insert:");
		  
			e.printStackTrace();
			
			System.out.println("afetr print statck trace----------------------------------------------------");
		}
		}
		System.out.println("after flush-----------------------");
		return flag;
	}

	@Transactional
	@Override
	public boolean insertUserTask(ProjectDTO projDto, TaskDTO taskDto, ReminderDTO reminderDTO, UserTaskDTO userTaskDTO)
			throws Exception {
		

		// TODO Auto-generated method stub\
		
		boolean flag = false;
		Task task = new Task();
		Reminder reminder = new Reminder();
		System.out.println("entitymanager = "+entityManager);
		for(int i= 0;i<userTaskDTO.getSignums().size();i++){
			System.out.println("userTaskDTO.getSignums().size()"+userTaskDTO.getSignums().size());
		User user = entityManager.find(User.class, userTaskDTO.getSignums().get(i));
		//Task task = new Task();
		//Project proj = new Project();
		
		Query query = entityManager.createQuery("select p from Project p where p.projectName = :projectName",Project.class);

		query.setParameter("projectName", projDto.getProjectName());
		List<Project> list = query.getResultList( );

		for( Project pr:list ) {
			int pId=pr.getProjectId();
			System.out.print("Project ID FROM LIST :" +pId );
		
		
		System.out.println("----------------------in saveusertask dao---------------------------------");
		System.out.println("Project id = "+pr.getProjectId());
		System.out.println("Project Name = "+pr.getProjectName());
		
		task.setProject(pr);
		task.setDueDate(taskDto.getDueDate());
		task.setPercentageComplete(taskDto.getPercentageComplete());
		task.setPriority(taskDto.getPriority());
		
		task.setStartDate(taskDto.getStartDate());
		task.setStatus(taskDto.getStatus());
		task.setSubject(taskDto.getSubject());
		
		task.setReminder(taskDto.getReminder());
		reminder.setDate(reminderDTO.getReminderDate());
		reminder.setTime(reminderDTO.getReminderTime());
		
		List<Reminder> reminderList = new ArrayList<Reminder>();
		reminderList.add(reminder);		
		
		reminder.setTask(task);
		
		task.setReminders(reminderList);
		
		
		//User user = new User();
		UserTask userTask = new UserTask();
		userTask.setTask(task);
		 
		
		
		userTask.setUser(user);
		try
		{
			//entityManager.persist(task);
			entityManager.persist(userTask);
			entityManager.flush();
			flag = true;
		}
		
		catch (PersistenceException p) {
			throw new Exception(p);
			
			
		}
		catch(Exception e)		
		{   System.out.println("error occured during insert:");
		  
			e.printStackTrace();
			
			System.out.println("afetr print statck trace----------------------------------------------------");
		}
		
		
		}
		
		}		
		//userTask.setUser(user);
		
		System.out.println("after flush-----------------------");
		return flag;
	
		
		
	//	return false;
	}
	
	

}
